package com.example.shoppingdemo.nslcommodity.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.javabean.OrderFormModel;
import com.example.shoppingdemo.nslcommodity.myview.RoundRectImageView;
import com.example.shoppingdemo.nslcommodity.orderbean.ListModel;

import java.util.List;

public class OrderFormAdpater extends RecyclerView.Adapter<OrderFormAdpater.OrderHolder>{
    private Context context;
    private onOrderParticulars orderParticulars;

    public void setOrderParticulars(onOrderParticulars orderParticulars) {
        this.orderParticulars = orderParticulars;
    }

    public OrderFormAdpater(Context context) {
        this.context = context;
    }
    private List<ListModel>orderFormModels;

    public void setOrderFormModels(List<ListModel> orderFormModels) {
        this.orderFormModels = orderFormModels;
    }

    @NonNull
    @Override
    public OrderHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        OrderHolder holder=new OrderHolder(LayoutInflater.from(context).inflate(R.layout.order_form_item,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull OrderFormAdpater.OrderHolder holder, int position) {
        Uri uri=Uri.parse(orderFormModels.get(position).getIcon());
        Glide.with(context).load(uri).into(holder.icon);
        holder.title.setText(orderFormModels.get(position).getTitle());
        String sKU=orderFormModels.get(position).getSku().substring(9,11)
                +":"+orderFormModels.get(position).getSku().substring(22,24)
                +","+orderFormModels.get(position).getSku().substring(35,37)
                +":"+orderFormModels.get(position).getSku().substring(48,51);
        holder.sku.setText(sKU);
        holder.money.setText("￥"+orderFormModels.get(position).getMoney());
        holder.numder.setText("X"+orderFormModels.get(position).getNumder());
        holder.activity.setText(orderFormModels.get(position).getActivity());
        holder.aMoney.setText("减 ￥"+orderFormModels.get(position).getaMoney());
        holder.aftersales.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orderParticulars.onOrderResult(holder.getAdapterPosition());
            }
        });
    }
    @Override
    public int getItemCount() {
        return null==orderFormModels?0:orderFormModels.size();
    }
    class OrderHolder extends RecyclerView.ViewHolder{
        RoundRectImageView icon;
        TextView title;
        TextView sku;
        TextView money;
        TextView numder;
        TextView activity;
        TextView aMoney;
        TextView aftersales;
        public OrderHolder(@NonNull View itemView) {
            super(itemView);
            icon=itemView.findViewById(R.id.image_order_form);
            title=itemView.findViewById(R.id.text_order_form_title);
            sku=itemView.findViewById(R.id.text_order_form_sku);
            money=itemView.findViewById(R.id.text_order_form_money);
            numder=itemView.findViewById(R.id.text_order_form_numder);
            activity=itemView.findViewById(R.id.text_order_form_item_huodong);
            aMoney=itemView.findViewById(R.id.text_order_form_item_jine);
            aftersales=itemView.findViewById(R.id.text_item_order_aftersales);
        }
    }
    public interface onOrderParticulars{
        void onOrderResult(int position);
    }
}
